<?php
namespace app\user\controller;
use think\Input;
use think\facade\Request;
class Financial extends Common{
    public function initialize(){
        parent::initialize();  
    }
    // 结算间
    public function index(){
        if(request()->isPost()){
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $userid=session('user.id');
            $list=db('jiesuan')->where('userid',$userid)
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['addtime'] = date('Y-m-d H:i',$v['addtime']); 
                if($v['state']===0){ 
                    $list['data'][$k]['money_s']='<span class="gre">+'.$v['money_s'].'</span>'; 
                    $list['data'][$k]['state_name']='<span class="red">待处理</span>'; 
                }else{
                    $list['data'][$k]['state_name']='<span class="green">已付款</span>'; 
                }
                if($v['type']=='user') $list['data'][$k]['money_a']='<span class="red">-'.$v['money_a'].'</span>';
                if($v['type']=='user'){
                    $list['data'][$k]['typename']='码商'; 
                    $list['data'][$k]['shopname']=$v['username']; 
                }else if($v['type']=='agent'){
                    $list['data'][$k]['typename']='代理'; 
                    $list['data'][$k]['shopname']=$v['username']; 
                }else if($v['type']=='shop'){
                    $list['data'][$k]['typename']='商户'; 
                    $list['data'][$k]['username']=$v['susername']; 
                    $list['data'][$k]['mobile']=$v['smobile']; 
                }
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }

        $this->assign('title','结算明细');
        return $this->fetch();
    }
    // 收款账号
    public function bank(){
    	if(request()->isPost()){
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $userid=session('user.id');
            $list=db('user_banks')->where('userid',$userid)->order('open')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['addtime'] = date('Y-m-d H:i',$v['addtime']); 
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }

        $this->assign('title','收款账号管理');
        return $this->fetch();
    }
    public function addbank(){
    	if(request()->isPost()) {
    		$data = Request::except('file');
            $data['addtime'] = time(); 
            $data['userid'] = session('user.id'); 
            $id = db('user_banks')->insertGetId($data);
            if($id){ 
                return array('code'=>1,'msg'=>'提交成功');
            }else{
            	return array('code'=>-1,'msg'=>'提交失败'); 
            }
    	}else{
	        $this->assign('title','新增收款账号');
	        return $this->fetch();
	    }
    }
    public function editState(){
        $id=input('post.id');
        $open=input('post.open');
        if(db('user_banks')->where('id='.$id)->update(['open'=>$open])!==false){
            return ['status'=>1,'msg'=>'设置成功!'];
        }else{
            return ['status'=>0,'msg'=>'设置失败!'];
        }
    } 
    // public function editbank(){
    //     if(Request::isAjax()) {
    //         $data = Request::except('file');
    //         db('ad')->update($data);
    //         $result['code'] = 1;
    //         $result['msg'] = '广告修改成功!';
    //         cache('adList', NULL);
    //         cache('paybanks', NULL); 
    //         $result['url'] = url('index');
    //         return $result;
    //     }else{
    //         $bankinfo=db('user_banks')->where(array('id'=>input('id')))->find(); 
    //         $this->assign('info',$bankinfo);
    //         return $this->fetch();
    //     }
    // }
    public function del(){
        if(db('user_banks')->where(array('id'=>input('id'),'userid'=>session('user.id')))->delete()!==false){
            return ['code'=>1,'msg'=>'删除成功！'];
        }else{
            return ['code'=>-1,'msg'=>'删除失败!'];
        }
    }

     public function editbank(){
        if(request()->isPost()) {
            $data = Request::except('file');
            $id=$data['id'];
            $bankinfos=db('user_banks')->where(array('id'=> $id))->find(); 
            if($data['name_en'] == 'Alipay'){
                $data['name_en'] = 'Alipay';
                $data['name'] = '支付宝';
            }elseif ($data['name_en'] == 'Wxpay') {
                $data['name_en'] = 'Wxpay';
                $data['name'] = '微信';
            }elseif ($data['name_en'] == 'Bank') {
                $data['name_en'] = 'Bank';
                $data['name'] = '银行卡';
            }
            if(empty($data['qrcode'])){
                unset($data['qrcode']);
            }
            // var_dump($bankinfos['qrcode']);die;
            //var_dump(db('user_banks')->where('id',$id)->fetchSql(true)->update($data));
            $update = db('user_banks')->where('id',$id)->update($data);
            if($update){ 
                return array('code'=>1,'msg'=>'提交成功');
            }else{
                return array('code'=>-1,'msg'=>'提交失败'); 
            }
        }else{
            $bankinfo=db('user_banks')->where(array('id'=>input('id')))->find(); 
            $this->assign('info',$bankinfo);
            return $this->fetch();
        }
    }
}